<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>组合模式</title>
</head>

<body>
    <script>
        // 场景 三代关系

        const Person = {
            getName(){
                return this.name
            },
            setName(){
                this.name = name 
            },
            display(){}
        }
        function Father(name){
            this.setName(name)
            var sons = []
            this.add = function(person){
                sons.push(person)
            }
            this.display = function(){
                console.log("作为父亲:"+this.getName())
                sons.forEach((son)=>{
                    son.display()
                })
            }
        }
        Father.prototype = Person
        function Son(name){
            this.setName(name)
            this.display = function(){
                console.log("作为儿子:"+this.getName());
            }
        }
        Son.prototype = Person

        var grandpa = new Father("老王的爷爷")
        var fa1 = new Father("老王的大伯")
        var fa2 = new Father("老王的爸爸")
        var fa3 = new Father("老王的妈妈")
        var fa4 = new Father("老王的哥哥")
        var fa5 = new Father("老王的弟弟")
        var fa6 = new Father("老王的姐姐")
        var fa7 = new Son("老王的妹妹")
        grandpa.add(fa1)
        grandpa.add(fa5)
        fa1.add(fa7)
        grandpa.display()
    </script>
</body>

</html>